// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RestoreRunner.cs" company="">
//   
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace ExecutionTool
{
    using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;

    using ExecutionTool.Common;
    using ExecutionTool.ImportExport;

    internal class RestoreRunner : RunnerBase
    {
        #region Constructors and Destructors

        public RestoreRunner(CommandLineOptions options)
            : base(options)
        {
        }

        #endregion

        #region Methods

        protected override void RunAction(Profile.Profile p)
        {
            /*
             * import & rename
             * send notification
             */
            var step1 = new ImportStep(
                p, 
                this.StorageAdapter, 
                this.ImportExportServiceAdapter, 
                this.SqlAzureAdapter, 
                null, 
                this.Options.RestoreBacpacName, 
                null, 
                string.IsNullOrWhiteSpace(this.Options.RestoreDatabaseRename) ? null : this.Options.RestoreDatabaseRename);
            ImportStepInfo importStepInfo;
            this.RunStep(step1, StepType.ImportRestore, out importStepInfo);

            // TODO: make sure retention process finished
            if (!this.Options.ToSuppressNotification && !string.IsNullOrWhiteSpace(p.Orchestration.NotificationList))
            {
                // TODO: send notification
            }

            this.Logger.LogInformation("Restore completed successfully.", 100);
        }

        protected override void TestConnectivityAction(Profile.Profile profile)
        {
            if (!profile.Orchestration.Export)
            {
                throw new InvalidOperationException(
                    "This profile doesn't support export, so a restore is not allowed. Correct the configuration before moving on.");
            }

            var tasks = new List<Task>();

            var sourceSqlAzureLocationTask = Task.Factory.StartNew(() => this.SqlAzureAdapter.TestConnection(profile.Source));
            tasks.Add(sourceSqlAzureLocationTask);

            if (profile.Orchestration.Export)
            {
                var importExportServiceTask =
                    Task.Factory.StartNew(
                        () =>
                        this.ImportExportServiceAdapter.TestConnection(
                            DataCenterMapper.GetDatacenterFromServerName(profile.Source.ServerName).Endpoint));
                tasks.Add(importExportServiceTask);

                var targetStorageLocationTask = Task.Factory.StartNew(() => this.StorageAdapter.TestConnection(profile.TargetAzureStorage));
                tasks.Add(targetStorageLocationTask);
            }

            Task.WaitAll(tasks.ToArray());
        }

        #endregion
    }
}
